package com.wenhuitec.sysui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RadioGroupAdapter extends RecyclerView.Adapter<RadioGroupAdapter.MyHolder> {

    List<Map<String, String>> mapList = new ArrayList<>();
    private Context mContext;
    private int mSelectPos = 0;

    public RadioGroupAdapter(Context context, List<Map<String, String>> list) {
        this.mContext = context;
        this.mapList = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_radio, parent, false);
        //将view传递给我们自定义的ViewHolder
        MyHolder holder = new MyHolder(view);
        //返回这个MyHolder实体
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.name.setText(mapList.get(position).get("name"));
        holder.radioContentBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //触发点击
                mSelectPos = position;
                notifyDataSetChanged();
            }
        });
        if (position == mSelectPos) {
            holder.radioSelectImg.setVisibility(View.VISIBLE);
        } else {
            holder.radioSelectImg.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mapList.size();
    }


    class MyHolder extends RecyclerView.ViewHolder {
        TextView name;
        LinearLayout radioContentBut;
        ImageView radioSelectImg;

        public MyHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.nameTv);
            radioContentBut = itemView.findViewById(R.id.radioContentBut);
            radioSelectImg = itemView.findViewById(R.id.radioSelectImg);
        }
    }
}
